import React, { Component } from 'react'
import {
  BrowserRouter as Router,
  Switch,
  Route,
  Redirect
} from 'react-router-dom'
import App from './App'
import { NotFound, Login } from './pages'
import { connect } from 'react-redux'
import { checkTokenAction } from './actions/user'
const mapState = state => {
  return {
    hasLogin: state.user.hasLogin
  }
}
@connect(
  mapState,
  { checkTokenAction }
)
export default class routeComponents extends Component {
  componentDidMount() {
    this.props.checkTokenAction()
  }
  render() {
    return (
      <Router>
        <Switch>
          <Route
            path="/admin"
            render={appProps => {
              if (this.props.hasLogin) {
                return <App {...appProps} />
              } else {
                return <Redirect to="/login" />
              }
            }}
          />
          <Route path="/login" component={Login} exact />
          <Route path="/404" component={NotFound} />
          <Redirect to="admin" from="/" exact />
          <Redirect to="/404" />
        </Switch>
      </Router>
    )
  }
}
/* export default () => (
  <Router>
    <Switch>
      <Route path="/admin" component={App} />
      <Route path="/login" component={Login} exact />
      <Route path="/404" component={NotFound} />
      <Redirect to="admin" from="/" exact />
      <Redirect to="/404" />
    </Switch>
  </Router>
) */
